package com.example.springexcel.JavaFx;

import com.example.springexcel.Excel.DataProcessing;
import com.example.springexcel.pojo.Vo.DataWranglingDataVo;
import com.example.springexcel.pojo.Vo.SequenceExcelDataVo;
import com.example.springexcel.utils.ExcelFileUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.example.springexcel.Excel.DataProcessing.RandomTypography;

@Slf4j
public class ViewService {
    public Label Txt;
    public Label Txt1;
    public RadioButton radioButton;

    File showOpenDialogFile = null;
    File showSaveDialogFile = null;

    @FXML
    public void SelectLocation(ActionEvent actionEvent) {
        showOpenDialogFile = DialogFile.getShowOpenDialogFile();
        if (showOpenDialogFile == null) {
            Txt.setText("文件选择位置：未选择文件");
            return;
        }
        Txt.setText("文件选择位置：" + showOpenDialogFile.getPath());

        showSaveDialogFile = DialogFile.getDirectoryChooserSelectDirectory();
        if (showSaveDialogFile == null) {
            Txt1.setText("文件保存位置：未选择文件");
            return;
        }
        Txt1.setText("文件保存位置：" + showSaveDialogFile.getPath());
    }

    public void StartExecuting(ActionEvent actionEvent) {

        if (showOpenDialogFile == null || !showSaveDialogFile.exists()) {
            DialogFile.AlertDialogBox(Alert.AlertType.ERROR, "文件选择位置：未选择文件", "未选择文件选择位置", "未选择文件", null);
            return;
        }
        if (showSaveDialogFile == null) {
            DialogFile.AlertDialogBox(Alert.AlertType.ERROR, "文件保存位置：未选择文件", "未选择文件保存位置", "未选择文件", null);
            return;
        }
        // 序列填充
        List<SequenceExcelDataVo> sequenceExcelDataVos = DataProcessing.SequenceFilling(showOpenDialogFile.getAbsolutePath());

        // 数据整理
        List<DataWranglingDataVo> sorting = DataProcessing.Sorting(showOpenDialogFile.getAbsolutePath());

        // 随机添加排版结果
        HashMap<String, List<String>> stringListHashMap = RandomTypography(showOpenDialogFile.getAbsolutePath());

        // 在需要的时候获取当前状态
        if (!radioButton.isSelected()) {
            // 结果文件路径
            String WriteFile = showSaveDialogFile.getAbsolutePath() + "/序列填充结果.xlsx";
            ExcelFileUtils.createExcel(WriteFile, sequenceExcelDataVos, "序列填充结果", SequenceExcelDataVo.class);

            String SortingWriteFile = showSaveDialogFile.getAbsolutePath() + "/数据整理结果.xlsx";
            ExcelFileUtils.createExcel(SortingWriteFile, sorting, "数据整理结果", DataWranglingDataVo.class);

            String TypesettingWriteFile = showSaveDialogFile.getAbsolutePath() + "/随机添加排版结果.xlsx";
            ExcelFileUtils.createExcel(TypesettingWriteFile, stringListHashMap, "随机添加排版结果");
        }else {

            List<List<String>> dataList = stringListHashMap.entrySet().stream()
                    .map(entry -> Stream.concat(
                                    Stream.of(entry.getKey()), // 键作为第一个元素
                                    entry.getValue().stream()   // 值列表作为后续元素
                            ).collect(Collectors.toList())
                    )
                    .toList();
            String WriteFile = showSaveDialogFile.getAbsolutePath() + "/填充结果.xlsx";

            ExcelFileUtils.ListCreateExcel(WriteFile,Map.of("序列填充",sequenceExcelDataVos,"数据整理",sorting,"随机添加排版结果",dataList));

        }
        DialogFile.AlertDialogBox(Alert.AlertType.INFORMATION, "Excel处理成功,保存位置"+showSaveDialogFile.getAbsolutePath(), "Excel处理成功", "处理成功", null);

    }
}
